package com.blrs.service.project.receivable.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import com.blrs.common.utils.SecurityUtils;
import com.blrs.domain.Receivable;
import com.blrs.domain.vo.BusinessMainClassVo;
import com.blrs.mapper.ReceivableMapper;
import com.blrs.service.project.receivable.IReceivableService;
import com.blrs.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-02-29
 */
@Service
public class ReceivableServiceImpl implements IReceivableService
{
    @Autowired
    private ReceivableMapper receivableMapper;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Receivable selectReceivableById(String id)
    {
        return receivableMapper.selectReceivableById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param receivable 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Receivable> selectReceivableList(Receivable receivable)
    {
        if(SecurityUtils.getUserId()!=1&&SecurityUtils.getUserId()!=2){
            receivable.setBelongTo(SecurityUtils.getOrgId());
        }
        return receivableMapper.selectReceivableList(receivable);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param receivable 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertReceivable(Receivable receivable)
    {
        return receivableMapper.insertReceivable(receivable);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param receivable 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateReceivable(Receivable receivable)
    {
        return receivableMapper.updateReceivable(receivable);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteReceivableByIds(String[] ids)
    {
        return receivableMapper.deleteReceivableByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteReceivableById(String id)
    {
        return receivableMapper.deleteReceivableById(id);
    }

    @Override
    public int editMainClass(BusinessMainClassVo businessMainClassVo) {
        boolean flag = businessMainClassVo.getReceivableId().contains(",");
        int count = 0;
        if (flag) {
            String[] receivableIds = businessMainClassVo.getReceivableId().split(",");
            for (int i = 0; i < receivableIds.length; i++) {
                Receivable receivable = receivableMapper.selectReceivableById(receivableIds[i]);
                receivable.setClassificationId(businessMainClassVo.getMainClassId());
                receivable.setOperateTime(new Timestamp(new Date().getTime()));
                receivable.setOperateId(SecurityUtils.getUserId());
                count = receivableMapper.updateReceivable(receivable);
            }
        } else {
            Receivable receivable = receivableMapper.selectReceivableById(businessMainClassVo.getReceivableId());
            receivable.setClassificationId(businessMainClassVo.getMainClassId());
            receivable.setOperateTime(new Timestamp(new Date().getTime()));
            receivable.setOperateId(SecurityUtils.getUserId());
            count =receivableMapper.updateReceivable(receivable);
        }
        return count;
    }
}
